package cn.jdy.plugins.service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.RedPacketReceive;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lutan
 * @title : 红包提现记录操作实现
 */
@Service
public class RedPacketReceiveService extends BaseDao<RedPacketReceive> {

	@Autowired
	private ConfService confService;

	/**
	 * 按条件查询
	 * 
	 * @param RedPacketReceive
	 * @return
	 */
	@Cacheable(value = "cache1", key = "'help_id' + #RedPacketReceive.id")
	public List<RedPacketReceive> query(RedPacketReceive redPacketReceive,String... date) {
		if (redPacketReceive == null)
			return super.findAll();
		List<RedPacketReceive> list = null;

		if (redPacketReceive.getiDisplayLength() > 0) {
			list = super.find(createFilter(redPacketReceive, date), redPacketReceive.getiDisplayStart(),
					redPacketReceive.getiDisplayLength());
		} else {
			list = super.find(createFilter(redPacketReceive, date));
		}

		return list;
	}

	@Cacheable(value = "cache1", key = "'help_count' + #RedPacketReceive.id")
	public Long count(RedPacketReceive RedPacketReceive) {
		return super.count(createFilter(RedPacketReceive));
	}

	/**
	 * 拼装条件
	 * 
	 * @param RedPacketReceive
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFilter(RedPacketReceive redPacketReceive, String... date) {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if (null != redPacketReceive.getLiveId()) {
			dc.add(Restrictions.eq("liveId", redPacketReceive.getLiveId()));
		}
		if (!StringUtil.isNullOrEmpty(redPacketReceive.getWatchUserId())) {
			dc.add(Restrictions.eq("watchUserId", redPacketReceive.getWatchUserId()));
		}
		if (!StringUtil.isNullOrEmpty(redPacketReceive.getWatchUsername())) {
			dc.add(Restrictions.like("watchUsername", redPacketReceive.getWatchUsername(),MatchMode.ANYWHERE));
		}
		if (date.length > 0 && !StringUtil.isNullOrEmpty(date[0])
				&& !StringUtil.isNullOrEmpty(date[1])) {
			dc.add(Restrictions.between("createTime", date[0], date[1]));
		}
		dc.addOrder(Order.desc("createTime"));
		return dc;
	}



}
